package com.zhihuichongcheng.application.ui.im.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;

/**
 * Created by 3water on 2016/11/7.
 */

public class ImChatGroupNameActivity extends BaseActivity {
    private EditText mEtGroupName;
    private String mGroupId,mGroupName;
    private String mNewGroupName;
    private Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_name_change);

        initView();
        initData();


    }

    private void initView()
    {
        mEtGroupName = (EditText)findViewById(R.id.et_groupName);
        setTitle("修改群名");
        setTvRight("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNewGroupName = mEtGroupName.getText().toString();
                if(!TextUtils.isEmpty(mNewGroupName)&&!mNewGroupName.equals(mGroupName)&&mNewGroupName.length()<10)
                {
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                            try {
                                EMClient.getInstance().groupManager().changeGroupName(mGroupId,mNewGroupName);//需异步处理
                                mHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        Intent intent = new Intent();
                                        intent.putExtra("title",mNewGroupName);
                                        setResult(Activity.RESULT_OK,intent);
                                        finish();
                                    }
                                });
                            } catch (HyphenateException e) {
                                e.printStackTrace();
                            }
                        }
                }).start();
                } else if(!TextUtils.isEmpty(mNewGroupName)&&mNewGroupName.length()>10)
                {
                    ToastOpt.createToast(mContext,"群名称不能超过10个字");
                }
                else
                {
                    ToastOpt.createToast(mContext,"群名不能为空或者相同");
                }
            }
        });
    }

    private void initData()
    {
        Intent intent = getIntent();
        if(intent!=null&&intent.hasExtra("title"))
        {
            mGroupName = intent.getStringExtra("title");
        }
        if(intent!=null&&intent.hasExtra("id"))
        {
            mGroupId = intent.getStringExtra("id");
        }
        mEtGroupName.setText(mGroupName);
    }
}
